<template>
  <div>
    <examination-answer
      :inHeight="'calc(100vh - 300px)'"
      @approvalAnswer="approvalAnswer"
      ref="examination"
      :loading="loading"
      :examInfo="examInfo"
      :msgInfo="msgInfo"
      :titleName="titleName"
      :endTime="null"
    ></examination-answer>
  </div>
</template>

<script>
import examinationAnswer from "./components/examinationAnswer";
import { updatePost_readmessage } from "@/api/home/message";
import { getExamPaper, approvalAnswer } from "@/api/exam/actionExam";

export default {
  name: "approvalAnswer",
  components: {
    examinationAnswer,
  },
  data() {
    return {
      titleName: "批改试题",
      examInfo: {},
      msgInfo: {},
      loading: false,
    };
  },
  watch: {
    "$route.query.keyId": {
      handler() {
        this.getExaminationAnswerList();
      },
      immediate: true,
    },
  },
  created() {},
  mounted() {},
  methods: {
    async approvalAnswer(arr) {
      const loading = this.$loading({
        lock: true,
        text: "正在提交",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.6)",
      });
      let res = await approvalAnswer(arr);
      if (res.code == 200) {
        loading.close();
        this.$modal.msgSuccess("提交成功！");
        this.$store.dispatch("tagsView/delView", this.$router.currentRoute);
        setTimeout(() => {
          this.$router.go(-1);
        }, 100);
      } else {
        loading.close();
      }
    },
    async getExaminationAnswerList() {
      console.log("批改页面加载");
      this.loading = true;
      let res;
      try {
        res = await getExamPaper(this.$route.query.keyId);
      } catch (e) {
        console.error(e);
      }
      if (res) {
        console.log(res);
        this.msgInfo = {
          studentInfo: res.studentInfo,
          taskInfo: res.taskInfo,
        };
        let examArr = res.examPaper.examPaperTitleList;
        //遍历各个大题
        for (const examArrElement of examArr) {
          for (const examArrElementElement of examArrElement.topicList) {
            if (
              examArrElementElement.topicType == 5 ||
              examArrElementElement.topicType == 4
            ) {
              if (!examArrElementElement.right) {
                examArrElementElement.scoreGot = "";
                examArrElementElement.right = true;
              }
            }
          }
        }
        let formatObj = {
          ...res,
          examPaperTitleList: examArr,
        };
        this.examInfo = formatObj;
        //需要清除已读消息
        if (this.$route.query.readMessageId) {
          if (formatObj.passing == -1 || formatObj.passing == 1) {
            this.updateReadmessage(this.$route.query.readMessageId);
          }
        }
        console.log(formatObj);
        this.loading = false;
      } else {
        this.loading = false;
      }
    },
    async updateReadmessage(id) {
      //更新消息状态
      let res = await updatePost_readmessage(id);
      console.log(res, ">>>", id);
    },
  },
};
</script>

<style scoped>
</style>
